find_package(Gettext REQUIRED)

macro(repoclean_gettext_create_translations _potFile _firstPoFileArg)
   set(_firstPoFile "${_firstPoFileArg}")
   set(_gmoFiles)
   get_filename_component(_potBasename ${_potFile} NAME_WE)
   get_filename_component(_absPotFile ${_potFile} ABSOLUTE)
   set(_addToAll)

   if(${_firstPoFile} STREQUAL "ALL")
      set(_addToAll "ALL")
      set(_firstPoFile)
   endif(${_firstPoFile} STREQUAL "ALL")

   foreach (_currentPoFile ${_firstPoFile} ${ARGN})
      get_filename_component(_absFile ${_currentPoFile} ABSOLUTE)
      get_filename_component(_abs_PATH ${_absFile} PATH)
      get_filename_component(_lang ${_absFile} NAME_WE)
      set(_gmoFile ${CMAKE_CURRENT_BINARY_DIR}/${_lang}.gmo)
      add_custom_command(
         OUTPUT ${_gmoFile}
         COMMAND ${GETTEXT_MSGFMT_EXECUTABLE} -o ${_gmoFile} ${_absFile}
         DEPENDS ${_absPotFile} ${_absFile}
      )

      install(FILES ${_gmoFile} DESTINATION share/locale/${_lang}/LC_MESSAGES RENAME ${_potBasename}.mo)
      set(_gmoFiles ${_gmoFiles} ${_gmoFile})
   endforeach (_currentPoFile )
   add_custom_target(translations ${_addToAll} DEPENDS ${_gmoFiles})
endmacro(repoclean_gettext_create_translations)

file(GLOB POFILES *.po)
repoclean_gettext_create_translations(repo-clean.pot ALL ${POFILES})
